package cn.tedu._06vehicle.controller;

import cn.tedu._06vehicle.base.response.JsonResult;
import cn.tedu._06vehicle.base.response.StatusCode;
import cn.tedu._06vehicle.mapper.VehicleMapper;
import cn.tedu._06vehicle.pojo.dto.VehhicleAddParam;
import cn.tedu._06vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._06vehicle.pojo.entity.Vehicle;
import cn.tedu._06vehicle.pojo.vo.UserVo;
import cn.tedu._06vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@RestController
@RequestMapping("v1/vehicle/")
@Tag(name="02-查询车辆列表")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("add")
    @Operation(summary = "添加车辆信息")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody @Range(min=1L,max=500L,
            message = "车辆id范围必须在1-500之间") VehhicleAddParam vehicleAddParam, HttpSession session){
        log.debug("添加车辆信息");
        UserVo userVo = (UserVo)session.getAttribute("user");

        if(userVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Vehicle vehicle= new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);

        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVo.getId());
        log.debug("vehicle="+vehicle);
        vehicleMapper.insertVehicle(vehicle);
            return JsonResult.ok();
    }
    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name="brand"),
            @Parameter(name="license"),
            @Parameter(name="vehicleListQuery",hidden = true)
    })

    public JsonResult ListVehicle(VehicleListQuery vehicleListQuery,HttpSession session){
        log.debug("查询车辆列表"+vehicleListQuery);
        UserVo userVo = (UserVo)session.getAttribute("user");
        if(userVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Long UserId=userVo.getId();
        List<VehicleListVO> list=vehicleMapper.listVehicle(vehicleListQuery,UserId);
        log.debug("查询结果:"+list);
        return JsonResult.ok(list);



    }
    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult delVehicle(@PathVariable @Range(min = 1L, max = 500L, message = "车辆id范围1-500") Long id,HttpSession session){
        //手动抛出异常
        if(id<=0){
            throw new IllegalArgumentException("车辆id不能小于0");
        }
        log.debug("删除车辆功能"+id);
        UserVo userVo=(UserVo)session.getAttribute("user");
        if(userVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Long userId=userVo.getId();
        int rows=vehicleMapper.deleteVehicle(id,userId);
        if(rows>0){
            return JsonResult.ok();
        }else return new JsonResult(StatusCode.OPERATION_FAILED);
    }



}
